package com.thti.mecool.client.command.result.plan;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.threeti.mecool.core.domain.model.plan.pojo.Plan;
import com.threeti.mecool.core.domain.model.plan.pojo.PlanJob;
import com.threeti.mecool.core.domain.model.plan.pojo.PlanProject;
import com.threeti.mecool.core.domain.model.plan.pojo.Step;
import com.threeti.mecool.core.domain.model.plan.pojo.StepItem;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.base.HandlerConstant;
import com.thti.mecool.client.command.ICommand;
import com.thti.mecool.client.model.biz.PlanBizImpl;
import com.thti.mecool.client.protocol.plan.PlansProtocolResult;
import com.thti.mecool.client.service.Protocol;
import com.thti.mecool.client.util.TimeUtil;

public class PlanCommand implements ICommand {
	PlanBizImpl planBizImpl;
	List<Map<String, Object[]>> list = new ArrayList<Map<String, Object[]>>();

	@Override
	public void doCommand(Object o) {
		PlansProtocolResult plansProtocolResult = (PlansProtocolResult) o;
		if (plansProtocolResult != null) {
			if (plansProtocolResult.getStatus() == 1) {
				List<Plan> plans = plansProtocolResult.getPlans();
				if (plans.size() > 0 && plans != null) {
					for (int n = 0; n < plans.size(); n++) {
						planBizImpl = new PlanBizImpl(Cache.getmContext());
						Plan planServer = plans.get(n);
						if (null != planServer) {
							String planId=String.valueOf(planServer.getId());
							if (!planBizImpl.findByPlanId(planId,BaseActivity.getCurrentUser().getId())) {
//								planBizImpl.delete(
//										String.valueOf(planServer.getId()),
//										true);
								list.add(savePlan(planServer));
							}
//							planBizImpl.deleteProject(planId, projectId, taskId);
							
							
							List<PlanProject> planProjects = planServer
									.getPlanProjects();
							if (planProjects.size() > 0) {
								String proId=null;
								for (int i = 0; i < planProjects.size(); i++) {
									if(proId==null || !proId.equals(planProjects.get(i).getProjectCode())){
										 if(!planBizImpl.findByProjectId(planId, planProjects.get(i).getProjectCode(), BaseActivity.getCurrentUser().getId()))
										 list.add(saveProject(planProjects.get(i)));
									}
									proId=planProjects.get(i).getProjectCode();

									List<PlanJob> planJobs = planProjects
											.get(i).getPlanJobs();
									if (planJobs.size() > 0) {
										for (int j = 0; j < planJobs.size(); j++) {
											if(!planBizImpl.findByTaskId(planId, proId, String.valueOf(planJobs.get(j).getId()), BaseActivity.getCurrentUser().getId())){
											list.add(saveTask(planJobs.get(j),
													planServer.getType()));
											List<Step> steps = planJobs.get(j)
													.getJob().getSteps();
											if (steps.size() > 0) {
												for (int k = 0; k < steps
														.size(); k++) {
													List<StepItem> stepItems = steps
															.get(k)
															.getStepItems();
													list.add(saveTaskStep(
															steps.get(k),
															null,
															String.valueOf(planProjects
																	.get(i)
																	.getProjectCode()),
															String.valueOf(planServer
																	.getId()),
															0,
															String.valueOf(planJobs
																	.get(j)
																	.getId()),
															String.valueOf(planJobs
																	.get(j)
																	.getId()),0));
													if (stepItems != null
															&& stepItems.size() > 0) {
														SaveStepItem(
																stepItems,
																String.valueOf(planProjects
																		.get(i)
																		.getProjectCode()),
																String.valueOf(planServer
																		.getId()),
																String.valueOf(planJobs
																		.get(j)
																		.getId()));
													}
												}
											}
										}}
									}
								}
							}
						}
					}
					if (null != list && list.size() > 0) {
						planBizImpl.savePlan(list);
					}
				}
				BaseActivity.setDataFlag(HandlerConstant.PlanCommand);
			}else{
				BaseActivity.setFailName("任务管理");
				BaseActivity.setDataFlag(HandlerConstant.CommandFail);
			}
		}

	}

	public void SaveStepItem(List<StepItem> stepItems, String projectId,
			String planId, String jobId) {
		if (stepItems.size() > 0 && stepItems != null) {
			for (int i = 0; i < stepItems.size(); i++) {
				List<Step> steps = stepItems.get(i).getSteps();
				if (steps.size() > 0 && steps != null) {
					list.add(saveTaskStep(null, stepItems.get(i), projectId,
							planId, 2,
							String.valueOf(stepItems.get(i).getStep().getId()),
							jobId,0));
					for (int j = 0; j < steps.size(); j++) {
						
						List<StepItem> stepItemss = steps.get(j).getStepItems();
						if (stepItemss.size() > 0 && stepItemss != null) {
							list.add(saveTaskStep(
									steps.get(j),
									null,
									projectId,
									planId,
									1,
									String.valueOf(steps.get(j).getStepItem()
											.getId()), jobId,0));
							SaveStepItem(stepItemss, projectId, planId, jobId);
						}else{
							list.add(saveTaskStep(
									steps.get(j),
									null,
									projectId,
									planId,
									1,
									String.valueOf(steps.get(j).getStepItem()
											.getId()), jobId,2));
						}
					}
				}else{
					list.add(saveTaskStep(null, stepItems.get(i), projectId,
							planId, 2,
							String.valueOf(stepItems.get(i).getStep().getId()),
							jobId,1));
				}
			}
		}
	}

	public Map<String, Object[]> savePlan(Plan planServer) {
		Map<String, Object[]> map = new HashMap<String, Object[]>();
		map.put(HandlerConstant.PLAN_SQL,
				new Object[] { BaseActivity.getCurrentUser().getId(),
						planServer.getId(), planServer.getName(), 0, 1,
						TimeUtil.dateToYYYYMM(planServer.getPlanDate()),
						planServer.getStatus(), planServer.getDenyNotes(),
						planServer.getChangeNotes(), planServer.getType(),
						new Date() });
		return map;
	}

	public Map<String, Object[]> saveProject(PlanProject planProject) {
		Map<String, Object[]> map = new HashMap<String, Object[]>();
		map.put(HandlerConstant.PROJECT_SQL,
				new Object[] { BaseActivity.getCurrentUser().getId(),
						planProject.getProjectCode(),
						planProject.getProjectName(),
						planProject.getPlan().getId(), "1", new Date() });
		return map;
	}

	public Map<String, Object[]> saveTask(PlanJob planJob, String type) {
		Map<String, Object[]> map = new HashMap<String, Object[]>();
		map.put(HandlerConstant.TASK_SQL,
				new Object[] { BaseActivity.getCurrentUser().getId(),
						planJob.getPlanProject().getProjectCode(),
						planJob.getId(), planJob.getJob().getName(),
						TimeUtil.dateToString(planJob.getStartTime(), 1),
						TimeUtil.dateToString(planJob.getEndTime(), 1),
						planJob.getStoreCode(), planJob.getStoreName(),
						planJob.getPlanProject().getPlan().getId(), type ,0});
		return map;
	}

	public Map<String, Object[]> saveTaskStep(Step step, StepItem stepItem,
			String projectId, String planId, int isType, String parentId,
			String jobId,int isOver) {
		Map<String, Object[]> map = new HashMap<String, Object[]>();
		if (isType != 2) {
			map.put(HandlerConstant.TASK_STEP_SQL, new Object[] { planId,
					projectId, jobId, step.getId(), parentId, isType,
					step.getTask().getActionId(),step.getName() , isOver});
		} else {
			map.put(HandlerConstant.TASK_STEP_SQL, new Object[] { planId,
					projectId, jobId, stepItem.getId(), parentId, isType,"-1",
					stepItem.getTaskItem().getName(),isOver});
		}
		return map;
	}

	@Override
	public String getCommandID() {
		// TODO Auto-generated method stub
		return Protocol.getPlan;
	}

	@Override
	public Class<?> getCommandClass() {
		// TODO Auto-generated method stub
		return PlansProtocolResult.class;
	}

}
